
var em_scan;
// 1 sequence for scaning 
// sol, arm, arm_c with x, y 
// result cb : 
CScaning = function() {

this.class_scaning = 1;
//hnt = this;
// define stage 
this.action_stage = new Array(3);

this.prevDA_index = 0;

// store this ref 
em_scan = this;
};


CScaning.DA_INVALID = 9;
CScaning.SOL_INVALID = 10;
CScaning.FAIL_ARMA_WCOST = 11;
CScaning.NOT_SUFFICENT_F = 12;
CScaning.NO_HERO = 13;
CScaning.ATT_MAX = 14;


CScaning.prototype = {

	doScan: function (scan_param) {
		// citinum, x, y , tr : {s:1} , success_cb, error_cb, notify_cb 
		this.param = scan_param;
		
		debug.log(this, retName(this), scan_param);
		// start 1st action 
		this.next_action_idx = 0; // reset index 
		this.goNextAction();
	},


	goNextAction: function() {
		// go next 
		this.next_action_idx += 1; 
		
		// done complete!
		if ( this.next_action_idx >= this.action_stage.length ) {
			debug.log(this, retName(this), ' Scan Action!! Complete ', this.travel_secs );
			this.param.success_cb( this.travel_secs );
			return;
		}
		
		
		debug.log(this, retName(this), this.action_stage[this.next_action_idx].cmd + ' start!! '
		,this.action_stage[this.next_action_idx].doaction
		,this);
	
	
	
		var outer = this;
		
		outer.action_stage[outer.next_action_idx].doaction.call(outer);
		
	},



doTest : function() {
this.goNextAction();
},

 


	checkSolEdu: function(data) {
		debug.log("got soldata in hunt " , data );
		
		var sol_valid = false;
		
		// check cur co odi is valid
		var sols = data.ret.soldiers;
		if ( em.scaning.param.tr.s || em.scaning.param.tr.s > 0 ) {
			s_valid = false;
			if ( sols[1][1] >= em.scaning.param.tr.s )
				s_valid = true; 
		} 
		
		sol_valid = s_valid ;
		
		if ( sol_valid == false ) {
			this.param.error_cb(CScaning.SOL_INVALID);
			// abort actions & nothing.. 
		} else {
			// go next action
			this.goNextAction();
		} 
	},

	gotSolEdu: function(soldata) {
	// change caller 
	em.scaning.checkSolEdu.call(em.scaning, soldata);
	},

	
	doSolEdu: function( ) {
	//&city=3070
	var param = { citnum: this.param.citnum, cb : this.gotSolEdu};
	em.ahelper.solEdu(param);
	},




	// cost 占쎌뮋�쇿뜝�숈삕占쏙퐦�쇿뜝占�
	checkArmament_at: function(data) {
		// ({"code":0,"ret":{"carry":6000,"cost_food":6000,"cost_wood":0,"cost_iron":0,"cost_gold":0,"distance":2400,"travel_sec":360}})
		this.cost = data.ret;
		if ( data.code != 0 ) {
			// invalid : target 
			debug_log( " invalid att ret code : expect f cost ")
			em.scaning.param.error_cb(CScaning.NOT_SUFFICENT_F);
		} else {
			
			em.scaning.goNextAction();
		}
	
	},

	gotArmament_at: function(data) {
	// change caller 
		em_scan.checkArmament_at.call(em.scaning, data);
	},

	doArmament_at: function( ) {
		this.ATTC = 3; // scan type 
		// att 
		/*
		&city=3070
		&action=do_war
		&attack_type=3
		&tai_num=3
		&area=184&area_x=53
		*/
		var param = { 
			citnum: this.param.citnum,
			atttype: this.ATTC , // 7 att , 3 : scan 
			genid : undefined, // not used in scan 
			tainum : this.param.tr.s, // scan : use tainum 
			troops : undefined, // 
			x: this.param.x, y: this.param.y,
			cost : this.cost, 
			cb : this.gotArmament_at 
		};
		
		this.param.notify_cb(" do scan " + param.x + ' / ' + param.y );
		em.ahelper.armament_action_att(param);
},

 

 


checkArmament_at_cost: function(data) {
	// ({"code":0,"ret":{"cd":[{"id":3235418,"cdtype":4,"target":7,"owner":210,"secs":360,"ret":0,"ext":"205\/248"}] } }) 
	this.hnt_result = data.ret.cd;
	if ( data.code != 0 ) {
		// invalid : target 
		debug.log( this, " invalid scan w cost ")
		this.param.error_cb(CScaning.FAIL_ARMA_WCOST);
	} else {
		debug.log(this,  " secs : " + data.ret.cd[0].secs);
		this.param.notify_cb(" secs : " + data.ret.cd[0].secs);
		this.travel_secs = data.ret.cd[0].secs;
		em.util.print_result(' travel time ::' + CUtil.TimeToString( new Date($.now() + 60* this.travel_secs)) );
		this.goNextAction(); // check completion at there 
	} 
},

gotArmament_at_cost: function(data) {
// change caller 
em_scan.checkArmament_at_cost.call(em_scan, data);
},

// arament_at �좎럥흮�묕옙�꾩룇猷뉐뜝占폺ost 占쎌뮋��占쎈툙�욤굢占썽쨹恝�숋옙占�
doArmament_att_wcost : function() {
this.ATTC = 3; // scan type

var param = { 
citnum: this.param.citnum,
atttype: this.ATTC , // 7 att , 3 : scan 
genid : undefined, // not used in scan 
tainum : this.param.tr.s, // scan : use tainum 
troops : undefined, // 
x: this.param.x, y: this.param.y,
cost: this.cost, 
cb : this.gotArmament_at_cost };

em.ahelper.armament_action_att_wcost(param);
},

 


initialize: function() {
debug_log( " Scaning init " );

var outer = this;
// define actions : outer �좑옙
this.action_stage = [
{cmd: "test" , doaction: outer.doTest },
{cmd: "sol_edu" , doaction: outer.doSolEdu},
{cmd: "armament_at" , doaction: outer.doArmament_at},
{cmd: "armament_at_cost" , doaction: outer.doArmament_att_wcost }, 
// {cmd: "getcitinfo" , doaction: outer.doGetCitInfo },
// {cmd: "getcdinfo" , doaction: outer.doGetCdInfo },

];
this.cur_action = ""; // empty ... reset 
this.next_action_idx = 0;
},

 

};

 

 


// /soldier_educate_api.php?jsonpcallback=jsonp1321974261717&_=1321974450455
//&key=a0ec2950f34992bd4d5db145cfea95d3
//&city=3070&_l=en&_p=EW-DROID

/*
2: sp
8 : lanu
15 : ov
17: k
jsonp1321974261717(
{"code":0,
"ret":
{"soldiers":
[[1,0,1],[2,121,1],[3,0,1],[4,0,1],[5,0,1],[6,0,1],[7,0,1],[8,1528,1],[9,0,1],[10,12,1],[11,0,1],[12,0,1],[13,0,1],[14,0,1],[15,2077,1],[16,0,1],[17,2699,1],[18,1,1]],
"space":6562,"head":8356,"def":1,"next":[0,0],"f":-38261}})
*/

 

/*
armament_action_do_api.php?
jsonpcallback=jsonp1321974261718&_=1321974460167&key=a0ec2950f34992bd4d5db145cfea95d3
&city=3070
&action=do_war
&attack_type=3
&tai_num=3
&area=184&area_x=53
&_l=en
&_p=EW-DROID


jsonp1321974261718(
{"code":0,
"ret":
{"carry":180,"cost_food":180,"cost_wood":0,"cost_iron":0,"cost_gold":0,"distance":2040,"travel_sec":0}})
*/

 

/*

get_cityinfo_api.php?
jsonpcallback=jsonp1321974267349&_=1321974460196&key=a0ec2950f34992bd4d5db145cfea95d3
&city=3070
&_l=en
&_p=EW-DROID


2 : remain area
192 : area
484741,21213527,2915768,21213527,220137,21213527,1075887,21213527, : resource cur / max
9652,9644, : popul
5,5,5,29, : farm
18,26,24,22,13,24,18, : structure town, bar, wall, arena, storage, research, fucility

jsonp1321974267349(
{"code":0,
"ret":
{"city":
[2,193,
484741,21213527,2915768,21213527,220137,21213527,1075887,21213527, //9
9652,9644, //11
5,5,5,29, //15
18,26,24,22,13,24,18, // 22
[{"id":39545,"itemid":166,"secs":118075}, //23
{"id":40536,"itemid":166,"secs":462108},
{"id":40819,"itemid":120,"secs":45057}]
,0],
"grade":62,"money":0}
}
)
*/


/*
// 
armament_action_task_api.php?
jsonpcallback=jsonp1321974261719&_=1321974468658&key=a0ec2950f34992bd4d5db145cfea95d3
&city=3070
&action=war_task
&attack_type=3&tai_num=3
&area=184&area_x=53
&_l=en
&_p=EW-DROID
&carry=180&cost_food=180&cost_wood=0&cost_iron=0&cost_gold=0&distance=2040&travel_sec=0


jsonp1321974261719(
{"code":0,
"ret":
{"cd":
[{"id":3281779,"cdtype":4,"target":3,"owner":0,"secs":0,"ret":0,"ext":"184\/53"}]
}
}
)

*/

 

/*
get_cdinfo_api.php?
jsonpcallback=jsonp1321974261720&_=1321974470536&key=a0ec2950f34992bd4d5db145cfea95d3
&city=3070
&_l=en
&_p=EW-DROID


jsonp1321974261720(
{"code":0,
"ret":
{"cdlist":
[
{"id":757395,"cdtype":1,"target":4,"owner":0,"secs":170134},
{"id":780811,"cdtype":1,"target":9,"owner":0,"secs":504194},
{"id":805491,"cdtype":0,"target":5,"owner":0,"secs":5220},
{"id":206987,"cdtype":2,"target":5,"owner":26106,"secs":58628}],
"grade":62,"money":0}}
)
*/


/*
message_api.php?
jsonpcallback=jsonp1321974261721&_=1321974484078&key=a0ec2950f34992bd4d5db145cfea95d3
&page=1
&_l=en
&_p=EW-DROID


jsonp1321974261721(
{"code":0,
"ret":
{"mail":
[
{"id":"473273","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321968210,"new":1},{"id":"471036","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09\uc2dc\uac04 \u2605\u2605","time":1321925649,"new":1},
{"id":"470279","sender":"sys","title":"Famine happens in Castle [185\/53]!","time":1321913471,"new":0},{"id":"469881","sender":"sys","title":"NO Food in Castle [185\/53]!","time":1321889675,"new":1},{"id":"469772","sender":"sys","title":"Eliminated by [Led] adam in PVP","time":1321886003,"new":1},{"id":"468104","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321842417,"new":1},
{"id":"468103","sender":"\uae40\uc120\uc9c4","title":"24\uc77c \ubaa9\uc694\uc77c 18\uc2dc \ud734\uc804\ud574\uc81c","time":1321838344,"new":0},{"id":"467545","sender":"sys","title":"[185\/53]\uc131 \ub0b4\uc5d0 \uc2dd\ub7c9\uc774 \uc5c6\uc2b5\ub2c8\ub2e4!","time":1321829813,"new":1},{"id":"466090","sender":"sys","title":"Resource Colony 4\/72 is expired","time":1321791198,"new":1},{"id":"467552","sender":"\ud751\uc778","title":"\ud751\uc778\ub18d\uc7a5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762518,"new":0},
{"id":"467551","sender":"\ud751\uc778","title":"\ud751\uc778\ub18d\uc7a5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762510,"new":0},{"id":"467550","sender":"\uae40\uc120\uc9c4","title":"\uc0ac\uce6d\uc8fc\uc758","time":1321762288,"new":0},{"id":"467549","sender":"\ud751\uc778","title":"\uc8fc \ud751\uc778\ub18d\uc7a5\uac1c\uc5c5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762278,"new":0},{"id":"467548","sender":"\ud751\uc778","title":"(\uc8fc) \ud751\uc778\ub18d\uc7a5 \uac1c\uc5c5","time":1321762183,"new":0},{"id":"467547","sender":"\ud751\uc778","title":"\ub2e4\ub4e4 \uc218\uc1a1\ud301 \ub2e4\uc2dc-\ucd5c\uc885\uc801","time":1321759625,"new":0},{"id":"467546","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321755594,"new":1},{"id":"462487","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321680488,"new":1},{"id":"462486","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321677413,"new":1},{"id":"462485","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321675795,"new":1},
{"id":"462484","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321675492,"new":1}
],
"max":2}})
*/


/*

/war_result_list_api.php?
jsonpcallback=jsonp1321974261722&_=1321974489374&key=a0ec2950f34992bd4d5db145cfea95d3
&page=1
&type=3
&_l=en
&_p=EW-DROID


jsonp1321974261722(
{"code":0,
"ret":
{"war":
[
{"id":3276196, // id !! 
"type":3,"aid":3074,"did":0,"aname":"james!!","dname":null,
"acid":3070,"dcid":0,
"acity":"\ucc9c\uc0c1\uc131",
"dcity":"\ucc9c\uc0c1\uc131",
"ax":185,"ay":53,

"dx":184,"dy":53, // coodi

"flag":1,
"time":1321974150,
"new":1}
],"max":1}})
*/


/*

war_result_info_api.php?
jsonpcallback=jsonp1321974261723&_=1321974497945&key=a0ec2950f34992bd4d5db145cfea95d3
&id=3276196 // id
&_l=en
&_p=EW-DROID

 

// Horror(694)
jsonp1321974261723({
"code":0,
"ret":
{
"war_report":null,
"scout_report":
{"flag":0,
"result":
"<b>[Hero]<\/b><br\/>ChaosSlave (Lvl.3)<br\/><br\/><b>[Troops]<\/b><br\/>Horror(694)<br>Attack(15)&nbsp;&nbsp;Defense(8)&nbsp;&nbsp;Health(80)<br><br>"},
"other_report":null}})
*/

 